﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Exception Handling Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using EntLibContrib.ExceptionHandling.Configuration.Design.Properties;

namespace EntLibContrib.ExceptionHandling.Configuration.Design
{
	/// <summary>
	/// Represents a <see cref="ErrorCodeEntry"/> configuration element.
	/// </summary>
	[Image(typeof(ConfigurationSourceElementNode))]
	[SelectedImage(typeof(ConfigurationSourceElementNode))]
	public class ErrorCodeEntryNode : ConfigurationNode
	{
		#region Fields
		private int errorCode;
		private string wrapExceptionTypeName;
		private string exceptionMessage;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the <see cref="ErrorCodeEntry"/> this node represents.
		/// </summary>
		/// <value>
		/// The <see cref="ErrorCodeEntry"/> this node represents.
		/// </value>
		[Browsable(false)]
		public ErrorCodeEntryData ErrorCodeEntry
		{
			get
			{
				ErrorCodeEntryData data = new ErrorCodeEntryData(this.Name);
				data.ErrorCode = this.errorCode;
				data.WrapExceptionTypeName = this.wrapExceptionTypeName;
				data.ExceptionMessage = this.exceptionMessage;
				return data;
			}
		}

		/// <summary>
		/// Gets or sets the error code.
		/// </summary>
		/// <value>The error code.</value>
		[SRDescription("ErrorCodeDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		public int ErrorCode
		{
			get { return this.errorCode; }
			set { this.errorCode = value; }
		}

		/// <summary>
		/// Gets or sets the name of the wrap exception type.
		/// </summary>
		/// <value>The name of the wrap exception type.</value>
		[SRDescription("WrapExceptionTypeNameDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		[Editor(typeof(TypeSelectorEditor), typeof(UITypeEditor))]
		[BaseType(typeof(Exception), TypeSelectorIncludes.BaseType)]
		public string WrapExceptionTypeName
		{
			get { return this.wrapExceptionTypeName; }
			set { this.wrapExceptionTypeName = value; }
		}

		/// <summary>
		/// Gets or sets the exception message.
		/// </summary>
		/// <value>The exception message.</value>
		[SRDescription("ExceptionMessageDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		public string ExceptionMessage
		{
			get { return this.exceptionMessage; }
			set { this.exceptionMessage = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="ErrorCodeEntryNode"/> class.
		/// </summary>
		public ErrorCodeEntryNode()
			: this(new ErrorCodeEntryData(Resources.ErrorCodeEntryNodeName))
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="ErrorCodeEntryNode"/> class with a <see cref="ErrorCodeEntry"/> instance.
		/// </summary>
		/// <param name="data">A <see cref="ErrorCodeEntry"/> instance</param>
		public ErrorCodeEntryNode(ErrorCodeEntryData data)
		{
			if (null == data)
				throw new ArgumentNullException("data");

			Rename(data.Name);
			this.errorCode = data.ErrorCode;
			this.wrapExceptionTypeName = data.WrapExceptionTypeName;
			this.exceptionMessage = data.ExceptionMessage;
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Releases the unmanaged resources used by the <see cref="ErrorCodeEntryNode"/> and optionally releases the managed resources.
		/// </summary>
		/// <param name="disposing">
		/// <see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.
		/// </param>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
			}
			base.Dispose(disposing);
		}
		#endregion
	}
}
